package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.annotation.MyLog;
import com.woniuxy.annotation.RequirePermission;
import com.woniuxy.entity.Emp;
import com.woniuxy.entity.Fault;
import com.woniuxy.service.fault.FaultService;
import com.woniuxy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class FaultController {
    @Autowired
    private FaultService faultService;



    @PostMapping("fault/findAll")
    public ResultVo findAll(@RequestBody Map<String, String> map) {
        String page = map.remove("page");
        String size = map.remove("size");
        PageInfo<Fault> all = faultService.findAll(Integer.valueOf(page), Integer.valueOf(size));
        return ResultVo.success(all);
    }

    @MyLog("修改故障信息")
    @RequirePermission("fault:update")
    @PostMapping("fault/update")
    public ResultVo update(@RequestBody Fault fault) {
        faultService.update(fault);
        return ResultVo.success();
    }

    @MyLog("删除故障信息")
    @RequirePermission("fault:delete")
    @PostMapping("fault/delete")
    public ResultVo delete(@RequestBody String fId) {
        int rows = faultService.delete(Integer.valueOf(fId));
        return rows > 0 ? ResultVo.success() : ResultVo.failure();
    }

    /**
     * 为故障分配维修人员
     * @return
     */
    @RequirePermission("emp:find")
    @MyLog("分配维修人员")
    @PostMapping("fault/distribute")
    public ResultVo distribute(@RequestBody Fault fault){
        faultService.distribute(fault);
        return ResultVo.success();
    }

    /**
     * 查询当地所有维修人员
     * @param bedId
     * @return
     */
    @PostMapping("fault/findDistributeEmp")
    public ResultVo findDistributeEmp(@RequestBody String bedId){
        List<Emp> list = faultService.findDistributeEmp(Integer.valueOf(bedId));
        return ResultVo.success(list);
    }
}
